package com.example.transation.controller;

import com.example.transation.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/trans")
public class TransController {
    @Autowired
    private UserService userService;
    /**
    程序异常，捕获并处理，这里结果就是提交
     */
    @Transactional
    @RequestMapping("t1")
    public Boolean register(String userName, String password){
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            //打印堆栈信息
            e.printStackTrace();
        }
        return true;
    }
    /**
    捕获异常之后，没有处理，直接再次抛出，回滚
     */
    @Transactional
    @RequestMapping("t2")
    public Boolean register1(String userName, String password){
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            throw e;
        }
        return true;
    }

    /**
     * 手动把事务进行回滚
     */
    @Transactional
    @RequestMapping("/t3")
    public Boolean r3(String userName, String password){
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }

    /**
     *  事务提交
     */
    @Transactional
    @RequestMapping("/t4")
    public Boolean r4(String userName, String password) throws IOException {
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if (true){
            throw new IOException();//为编译时异常
        }

        return true;
    }

    /**
     *  事务回滚
     */
    @Transactional
    @SneakyThrows
    @RequestMapping("/t5")
    public Boolean r5(String userName, String password)  {
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if (true){
            throw new IOException();
        }
        //在反编译中这里会捕获异常，并再次抛出
        return true;
    }

    /**
     *  事务回滚
     */
    @Transactional
    @RequestMapping("/t6")
    public Boolean r6(String userName, String password)  {
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if (true){
            //这里抛出的就是运行时异常，需要进行回滚
            throw new RuntimeException();
        }

        return true;
    }

    /**
     *  事务回滚
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
    //设置所有回滚异常类型
    @RequestMapping("/r7")
    public Boolean r7(String userName, String password) throws IOException {
        Integer result = userService.registUser(userName, password);
        System.out.println("插入用户表, result: "+ result);
        if (true){
            throw new IOException();
        }

        return true;
    }


}
